#define _CRT_SECURE_NO_WARNINGS 1
class Solution {
    typedef pair<int, int> PII;
    int dx[4] = { 0,0,1,-1 };
    int dy[4] = { -1,1,0,0 };
    bool vis[51][51];
public:
    int maxAreaOfIsland(vector<vector<int>>& grid) {
        int m = grid.size();
        int n = grid[0].size();
        int s = 0;
        for (int i = 0;i < m;i++)
        {
            for (int j = 0;j < n;j++)
            {
                if (!vis[i][j] && grid[i][j] == 1)
                {
                    s = max(s, bfs(grid, i, j));
                }
            }
        }
        return s;
    }
    int bfs(vector<vector<int>>& grid, int sr, int sc)
    {
        int m = grid.size();
        int n = grid[0].size();
        queue<PII> q;
        q.push({ sr,sc });
        vis[sr][sc] = true;
        int ret = 1;
        while (!q.empty())
        {
            auto [a, b] = q.front();
            q.pop();
            for (int i = 0;i < 4;i++)
            {
                int x = a + dx[i];
                int y = b + dy[i];
                if (x >= 0 && x < m && y >= 0 && y < n && !vis[x][y] && grid[x][y] == 1)
                {
                    q.push({ x,y });
                    vis[x][y] = true;
                    ret++;
                }
            }
        }
        return ret;
    }
};